package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.build.BuildLevelUpTemplate;
import com.xcity.game.build.BuildTemplate;
import com.xcity.game.item.ItemInfo;
import com.xcity.game.player.PlayerUtils;
import com.xcity.game.vip.VipTemplate;
import com.xcity.game.vip.VipUtils;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@Deprecated
@PomeloMessage(type = Message.Type.RESPONSE)
public class BuildListResponse extends Response {
	
	public int buildQueueSize;
	public int buyBuildQueueCost;
	public List<Building> builds;
	
	public static class Building {
		public int id;
		public String name;
		public String img;
		public long cost; // 解锁价格
		public byte type;
		public int buildId;
		public int income;
		public int maxIncome;
		public int cdTime;
		public int health; // 风险值
		public int price; // 建造价格
		public byte buyType;
		public int needLvl; // 角色等级限制
		public boolean unLock;
		public List<ItemReq> needItems; // 建造所需的物品
	}
	
	public static class ItemReq {
		public int id;
		public int count;
		public String name;
		public String icon;
		public String desc;
		public byte state; // 0:背包没有足够的该物品; 1:背包有足够的该物品
	}
	
	public BuildListResponse(int serial, Player player, List<BuildTemplate> templates) {
		super(serial);
		int size = templates.size();
		List<Building> list = new ArrayList<Building>(size);
		for (int i = 0; i < size; i++) {
			BuildTemplate temp = templates.get(i);
			if (player.getBuildings().hasBuild(temp.getId())) {
				continue;
			}
			BuildLevelUpTemplate lv1Temp = temp.getLevelUpTemplate(1);
			Building build = new Building();
			build.id = temp.getId();
			build.name = temp.getName();
			build.img = temp.getImage();
			build.cost = temp.getUnlockPrice();
			build.type = temp.getGroup();
			build.buildId = temp.getId();
			build.income = lv1Temp.getIncome();
			build.maxIncome = lv1Temp.getMaxIncome();
			build.cdTime = lv1Temp.getCd() * 1000;
			build.health = lv1Temp.getRisk();
			build.price = lv1Temp.getCost();
			build.buyType = temp.getCostType();
			build.needLvl = temp.getPlayerLevel();
			build.unLock = player.getBuildings().isUnlock(temp);
			
			ItemInfo[] items = lv1Temp.getNeedItems();
			if (items == null || items.length == 0) {
				build.needItems = Collections.emptyList();
			} else {
				build.needItems = new ArrayList<ItemReq>(items.length);
				for (ItemInfo item : items) {
					ItemReq req = new ItemReq();
					req.id = item.getId();
					req.count = item.getCount();
					req.name = item.getTemplate().getName();
					req.icon = item.getTemplate().getIcon();
					req.desc = item.getTemplate().getDesc();
					req.state = (byte) (player.getBag().hasEnough(req.id, req.count) ? 1 : 0);
					build.needItems.add(req);
				}
			}
			
			list.add(build);
		}
		this.builds = list;
		this.buildQueueSize = PlayerUtils.getBuildQueueMaxSize(player);
		VipTemplate vt = VipUtils.getTemplate(player.getVipLvl());
		this.buyBuildQueueCost = vt.getBuyBuildQueueCost();
	}
	
}
